package au.chao.main;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import au.chao.utils.GraphUtility;
import au.chao.utils.TenantBillReport;

public class MainFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8890382616970040170L;

	private JTable tblTenant = null;

	private JTable tblBill = null;

	public MainFrame() {
		initMenu();
		initGUI();
	}

	private void initMenu() {

		JMenuBar menuBar = new JMenuBar();

		// Create a menu
		JMenu menuFile = new JMenu("File");

		// Create a menu item
		JMenuItem itemLoad = new JMenuItem("Load File");
		JMenuItem itemSave = new JMenuItem("Save File");
		JMenuItem itemExit = new JMenuItem("Exit");
		itemExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		menuFile.add(itemLoad);
		menuFile.add(itemSave);
		menuFile.add(itemExit);

		JMenu menuReport = new JMenu("Report");
		JMenuItem rptCreate = new JMenuItem("Generate Report");
		JMenuItem rptSave = new JMenuItem("Save As Excel");
		menuReport.add(rptCreate);
		menuReport.add(rptSave);

		JMenu menuHelp = new JMenu("Help");
		JMenuItem itemAbout = new JMenuItem("About");
		itemAbout.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				JOptionPane.showMessageDialog(null,
						"Application Name: Share Bills\n"
								+ "Author: Steven Jiang\n"
								+ "Email: StevenJiang.au@gmail.com\n"
								+ "CopyRight SJ Software 2007", "Share Bills",
						JOptionPane.PLAIN_MESSAGE);

			}
		});
		menuHelp.add(itemAbout);

		menuBar.add(menuFile);
		menuBar.add(menuReport);
		menuBar.add(menuHelp);

		setJMenuBar(menuBar);

	}

	private void initGUI() {

		GraphUtility.setExistOnClose(this);
		setTitle("Shared Bills");
		setLayout(new GridBagLayout());

		TenantTableModel ttm = new TenantTableModel();
		for (String[] t : TenantDataLoader.row) {
			ttm.addRow(t);
		}
		tblTenant = new JTable(ttm);
		JScrollPane jspanelTenant = new JScrollPane(tblTenant);

		GridBagConstraints csTenant = new GridBagConstraints();
		csTenant.anchor = GridBagConstraints.NORTH;
		csTenant.weightx = 1;
		csTenant.weighty = 0.5;
		csTenant.gridx = 0;
		csTenant.gridy = 0;
		csTenant.fill = GridBagConstraints.BOTH;
		getContentPane().add(jspanelTenant, csTenant);

		Container boxTenant = new Container();
		GridLayout btnGlay = new GridLayout(3, 0);
		btnGlay.setVgap(5);

		boxTenant.setLayout(btnGlay);
		JButton btnAddTenant = new JButton("Add");
		btnAddTenant.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				new TenantEnterForm(tblTenant,0).setVisible(true);
			}

		});
		
		JButton btnEditTenant = new JButton("Edit");
		btnEditTenant.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				editTenant(1);
			}
			
		});
		
		JButton btnRemoveTenant = new JButton("Remove");
		btnRemoveTenant.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				removeTenant();
			}

		});
		
		boxTenant.add(btnAddTenant, BorderLayout.CENTER);
		boxTenant.add(btnEditTenant, BorderLayout.CENTER);
		boxTenant.add(btnRemoveTenant, BorderLayout.CENTER);

		GridBagConstraints csTenantBtn = new GridBagConstraints();

		csTenantBtn.anchor = GridBagConstraints.CENTER;
		csTenantBtn.weightx = 0;
		csTenantBtn.weighty = 1;
		csTenantBtn.gridx = 1;
		csTenantBtn.gridy = 0;
		csTenantBtn.fill = GridBagConstraints.HORIZONTAL;
		getContentPane().add(boxTenant, csTenantBtn);

		GridBagConstraints cs = new GridBagConstraints();
		cs.anchor = GridBagConstraints.NORTH;
		cs.weightx = 1;
		cs.weighty = 0.5;
		cs.gridx = 0;
		cs.gridy = 0;
		cs.fill = GridBagConstraints.BOTH;
		getContentPane().add(jspanelTenant, cs);

		BillTableModel btm = new BillTableModel();
		for (String[] b : BillDataLoader.row) {
			btm.addRow(b);
		}

		tblBill = new JTable(btm);
		JScrollPane jspanelBill = new JScrollPane(tblBill);

		GridBagConstraints cs1 = new GridBagConstraints();
		cs1.anchor = GridBagConstraints.CENTER;
		cs1.weightx = 1;
		cs1.weighty = 0.5;
		cs1.gridx = 0;
		cs1.gridy = 1;
		cs1.fill = GridBagConstraints.BOTH;
		getContentPane().add(jspanelBill, cs1);

		Container boxBill = new Container();
		boxBill.setLayout(btnGlay);
		
		JButton btnAddBill = new JButton("Add");
		btnAddBill.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				new BillEnterForm(tblBill,0).setVisible(true);
			}

		});
		
		JButton btnEditBill = new JButton("Edit");
		btnEditBill.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				editBill(1);
			}

		});
		
		JButton btnRemoveBill = new JButton("Remove");
		btnRemoveBill.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				removeBill();
			}

		});
		
		boxBill.add(btnAddBill);
		boxBill.add(btnEditBill);
		boxBill.add(btnRemoveBill);

		GridBagConstraints csBillBtn = new GridBagConstraints();

		csBillBtn.anchor = GridBagConstraints.CENTER;
		csBillBtn.weightx = 0;
		csBillBtn.weighty = 1;
		csBillBtn.gridx = 1;
		csBillBtn.gridy = 1;
		csBillBtn.fill = GridBagConstraints.HORIZONTAL;
		getContentPane().add(boxBill, csBillBtn);

		Box bottomBox = Box.createHorizontalBox();

		JButton btnLoad = new JButton("Load");
		btnLoad.setToolTipText("Load XML Content to Tables");
		bottomBox.add(btnLoad);
		bottomBox.add(Box.createHorizontalStrut(5));

		JButton btnSave = new JButton("Save");
		btnSave.setToolTipText("Save Content of Tables to Disk");
		bottomBox.add(btnSave);
		bottomBox.add(Box.createHorizontalStrut(5));

		JButton btnRpt = new JButton("Report");
		btnRpt.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				new TenantBillReport(tblTenant, tblBill).outputToFile(".","Bill-Report.txt");
				try {
					Runtime.getRuntime().exec("notepad Bill-Report.txt");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("System call failed");
				}

			}
			
		});
		btnRpt.setToolTipText("Create Report in Popup Winodw");
		bottomBox.add(btnRpt);
		bottomBox.add(Box.createHorizontalStrut(5));

		JButton btnExcel = new JButton("Export");
		btnExcel.setToolTipText("Export to Excel File");
		bottomBox.add(btnExcel);

		JButton btnExit = new JButton("Exit");
		btnExit.setToolTipText("Programme Exit");
		btnExit.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				System.exit(1);
			}

		});
		bottomBox.add(Box.createHorizontalGlue());
		bottomBox.add(btnExit);

		GridBagConstraints btnRptCS = new GridBagConstraints();
		btnRptCS.anchor = GridBagConstraints.LAST_LINE_END;
		btnRptCS.weightx = 0;
		btnRptCS.weighty = 0;
		btnRptCS.gridx = 0;
		btnRptCS.gridy = 2;
		btnRptCS.fill = GridBagConstraints.HORIZONTAL;
		btnRptCS.insets = new Insets(5, 5, 5, 5);
		getContentPane().add(bottomBox, btnRptCS);

		pack();
		setSize(600, 500);
		GraphUtility.centerWindow(this);

		setVisible(true);
	}

	private void editTenant(int addOrEdit){
		int selectedRowNum = tblTenant.getSelectedRow();
		if (selectedRowNum == -1) {
			JOptionPane.showMessageDialog(null, "Select A Row to delete",
					"alert", JOptionPane.ERROR_MESSAGE);
		} else {
	
			TenantEnterForm tenantForm = new TenantEnterForm(tblTenant, addOrEdit);
			tenantForm.populateData();
			tenantForm.setVisible(true);
			
		}
	}
	private void removeTenant() {
		int selectedRowNum = tblTenant.getSelectedRow();
		if (selectedRowNum == -1) {
			JOptionPane.showMessageDialog(null, "Select A Row to delete",
					"alert", JOptionPane.ERROR_MESSAGE);
		} else {
			if(JOptionPane.showConfirmDialog(null, 
					"Are you use to delete?", "Confirm Delete", JOptionPane.YES_NO_OPTION) == 0){
				((TenantTableModel)tblBill.getModel()).removeRow(selectedRowNum);	
			}
		}
	}


	private void editBill(int addOrEdit){
		int selectedRowNum = tblBill.getSelectedRow();
		if (selectedRowNum == -1) {
			JOptionPane.showMessageDialog(null, "Select A Row to delete",
					"alert", JOptionPane.ERROR_MESSAGE);
		} else {
			BillEnterForm billForm = new BillEnterForm(tblBill,addOrEdit);
			billForm.populateData();
			billForm.setVisible(true);
		}

		
	}
	
	private void removeBill() {
		int selectedRowNum = tblBill.getSelectedRow();
		if (selectedRowNum == -1) {
			JOptionPane.showMessageDialog(null, "Select A Row to delete",
					"alert", JOptionPane.ERROR_MESSAGE);
		} else {
			if(JOptionPane.showConfirmDialog(null, 
					"Are you use to delete?", "Confirm Delete", JOptionPane.YES_NO_OPTION) == 0){
				((BillTableModel)tblBill.getModel()).removeRow(selectedRowNum);	
			}
		}
	}	
	
	public JTable getTblBill() {
		return tblBill;
	}

	public void setTblBill(JTable tblBill) {
		this.tblBill = tblBill;
	}

	public JTable getTblTenant() {
		return tblTenant;
	}

	public void setTblTenant(JTable tblTenant) {
		this.tblTenant = tblTenant;
	}

}
